package com.tensquare.article.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.dao.ColumnDao;
import com.tensquare.article.pojo.Column;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

@Service
public class ColumnService {
    @Autowired
    private ColumnDao columnDao;
    @Autowired
    private IdWorker idWorker;

    public void save(Column column) {
        column.setId(idWorker.nextId()+"");
        columnDao.insert(column);
    }

    public List<Column> findAll() {
        return columnDao.selectList(null);
    }

    public Column findById(String columnId) {
        return columnDao.selectById(columnId);
    }

    public void updateById(Column column) {
        columnDao.updateById(column);
    }

    public void deleteById(String columnId) {
        columnDao.deleteById(columnId);
    }

    public Page<Column> findByPage(Integer page, Integer size, Map<String, Object> map) {
       //设置查询条件且封装
        EntityWrapper<Column> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        //设置分页条件
        Page<Column> page1=new Page<>(page,size);
        List<Column> list = columnDao.selectPage(page1, wrapper);
       page1.setRecords(list);
       return page1;
    }

    public List<Column> findByOne(Map<String,Object> map) {
        EntityWrapper<Column>wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        List<Column> list = columnDao.selectList(wrapper);
        return list;
    }
}
